/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package JKSerialization;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.ObjectOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;

import JKPanels.TestResultsPanel;

/**
 *
 * Class used to statically write the TestResultsPanel to the system
 *
 * @author Royce Feng
 */
public class JKObjectWriter {

    private JKObjectWriter() {}

    /**
     *
     * Writes the TestResultsPanel to the System. Will automatically create the
     * %PROGRAMFILES%\KTest directory if it does not exist.
     *
     * @param panel The TestResultsPanel
     */
    public static void writeResults ( TestResultsPanel panel ) {
        try {
            String filePath = System.getenv("PROGRAMFILES") + "\\KTest";
            File dir = new File(filePath);
            if ( !dir.exists() ) {
                dir.mkdir();
            }
            filePath += "\\Results.JKDATA";
            FileOutputStream fos = new FileOutputStream(filePath);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(panel);
            oos.flush();
        } catch ( FileNotFoundException e ) {
            JOptionPane.showMessageDialog(null, "Program could not store results. Please contact the developer.",
                    "Program Error", JOptionPane.ERROR_MESSAGE);
        } catch ( IOException e ) {
            JOptionPane.showMessageDialog(null, "Program could not store results. Please contact the developer.",
                    "Program Error", JOptionPane.ERROR_MESSAGE);
        }
    }

}
